home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11407 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.9 KB  |  121 lines

  1. Path: ix.netcom.com!news
  2. From: c.koontz@ix.netcom.com(Curtis Alan Koontz)
  3. Newsgroups: comp.lang.c
  4. Subject: Help with SORTING!!!!
  5. Date: 23 Mar 1996 22:05:18 GMT
  6. Organization: Netcom
  7. Message-ID: <4j1siu$4dc@dfw-ixnews2.ix.netcom.com>
  8. NNTP-Posting-Host: vic-ca1-19.ix.netcom.com
  9. X-NETCOM-Date: Sat Mar 23  4:05:18 PM CST 1996
  10.  
  11. Hello out there in C land.
  12.  
  13. I am in need of help getting my sort algorithm below working.
  14. everything seem to work alright but the "if ... then" statement.
  15. I had it working with arrays but cannot get it working with link
  16. lists. not all of the class is included just what you might need
  17. to help me find my ERROR or bug. I thank you one and all for any
  18. help you can offer me to solve my problem. I did not include node
  19. greate (new) or delete functions.  
  20.  
  21. Last node 'next' and first 'node' prev is set to NULL. I can
  22. traverse the list with no trouble. When the cur_->d.t.Grade
  23. change all the future nodes change to that record.
  24.  
  25. If someone has some linklist classes and are willing to share
  26. with me, I will be very thankful.  The one below is my first try
  27. at linklists.
  28.  
  29. System: 386-25 w/4meg. using Borland C/C++ 3.1, large memory
  30. module. Will be porting to linux for use very soon.
  31.  
  32. One more question. I found the hard way and have read thereafter.
  33. That when a node is delete or freed the memory is returned to
  34. program but is not available for new nodes to be greated from.
  35. can some explain why this is so? Example: the program I am making
  36. read a list of ticks then load the stock prices into another list
  37. or array. The price record count change (30-4000) for each tick.
  38. If I delete the list and make a new one for the current record
  39. count the memory from the deleted list is not used for the new
  40. list, so I quickly run out of memory in DOS.
  41.  
  42. //  code I need help with below
  43.  
  44. struct tickinfo     {
  45.      int count;
  46.      char group;
  47.      long loc;
  48.      TickNameStruct t;
  49.      };
  50. typedef tickinfo tickdata;
  51.  
  52. struct StructNode   {         //StructNode
  53.      tickdata d;
  54.      StructNode     *next;
  55.      StructNode     *prev;
  56.      };                  // NodeStruct;
  57. typedef StructNode NodeStruct;
  58.  
  59.  
  60. class classtick  {
  61.   NodeStruct *cur_, *first_, *last_, *temp_;
  62.   int node_;
  63.   short _cmptick(void)  {return(strcmp(cur_->d.t.TickS,
  64. temp_->d.t.TickS));}
  65.   short _cmpgrade(void) {return(strcmp(cur_->d.t.Grade,
  66. temp_->d.t.Grade));}
  67. public:
  68.   NodeStruct   *first(void)        {return(first_);}
  69.   NodeStruct   *last(void)         {return(last_);}
  70.   NodeStruct   *getnode(NodeStruct *first, int num);
  71.   NodeStruct   *getnode(int num)   {return(getnode(first(), num));}
  72.   int          nodecount(void);
  73.   int          nodes(void)         {return(node_);}
  74.   void         sortlist(void);
  75.   };
  76.  
  77. int  classtick::nodecount(void)         {
  78.      int cnt = 0;
  79.      for(temp_=first(); temp_!=last()->next; temp_=temp_->next)
  80.           ++cnt;
  81.      node_ = cnt;    //set count of max nodes in list
  82.      return(cnt);
  83.      }
  84.  
  85.  
  86.  
  87. void classtick::sortlist(void)     {    //HELP WITH SORTING!!!!
  88.   NodeStruct *xcur, *Tcur;
  89.   for(cur_=first(); cur_!=NULL; cur_=cur_->next)  {  //working OK
  90.     printf("check for dup's on %s %s\n", cur_->d.t.TickS,
  91. cur_->d.t.Grade);
  92.     for(temp_=cur_; temp_!=NULL; temp_=temp_->next)
  93.       if (!_cmptick() && !_cmpgrade() &&     (temp_!=cur_))
  94.         killnode(temp_);
  95.       }
  96.   int ii=0, bottom, top, middle, j;
  97.   nodecount();      //node correct count for max nodes
  98.   for(ii=1; ii<nodes(); ii++) {
  99.     temp_ = getnode(ii);
  100.     printf("sorting on %s %s\n", temp_->d.t.TickS, temp_->d.t.Grade);
  101.     bottom = 0;
  102.     top = ii-1;
  103.     while (bottom<=top)  {
  104.       middle = (bottom+top)/2;
  105.       cur_ = getnode(middle);    //working OK in tests
  106.       if ((_cmptick()>0) || (_cmpgrade()>0))    //this not working  <<<
  107.         top = middle-1;
  108.       else
  109.         bottom = middle+1;
  110.       }
  111.     for(j=ii-1;j>=bottom;j--) {
  112.       cur_ = getnode(j);
  113.       xcur = cur_->next;
  114.       xcur->d = cur_->d;
  115.       }
  116.     cur_ = getnode(bottom);
  117.     cur_->d = temp_->d;
  118.     }
  119.   }
  120.  
  121.